package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.host.dao.HostApplyDao;
import ysq.xyg.host.model.po.HostApply;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostApplyVo;
import ysq.xyg.host.service.HostApplyService;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 房产模块-申请经纪人service实现
 *
 * @author shengQiang yu
 * @date 2019-7-5 22:09:46
 */
@Service("hostApplyService")
public class HostApplyServiceImpl extends BaseServiceImpl<HostApply> implements HostApplyService {
    @Resource
    private WebMemberService webMemberService;
    @Resource
    private HostApplyDao hostApplyDao;

    private HostApply hostApply;

    @Autowired
    public void setBaseDao(HostApplyDao hostApplyDao) {
        this.baseDao = hostApplyDao;
    }

    /**
     * 查询集合
     *
     * @param hostAdviseVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:08:52
     */
    public List<HostApply> listLeft(HostApplyVo hostAdviseVo) {
        List<HostApply> list = hostApplyDao.listLeft(hostAdviseVo);
        if(list!=null&&list.size()>0){
            for (HostApply hostAdvise:list){
                if(StrUtil.isNotEmpty(hostAdvise.getNickName())){
                    hostAdvise.setNickName(BaseUtil.decodeBase64(hostAdvise.getNickName()));
                }
            }
        }
        return list;
    }
    /**
     * 分页查询
     *
     * @param hostApplyVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:09:46
     */
    public ReturnEntity page(HostApplyVo hostApplyVo, int pageSize, int pageNum) {
        if(StrUtil.isNotEmpty(hostApplyVo.getNickName())){
            hostApplyVo.setNickName(BaseUtil.encodeBase64String(hostApplyVo.getNickName()));
        }
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostApply> list = listLeft(hostApplyVo);
        PageInfo<HostApply> page=new PageInfo<HostApply>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostApplyVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:09:46
     */
    public ReturnEntity addOrUpdate(HostApplyVo hostApplyVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostApplyVo.getId())){
            hostApply=new HostApply(currentUser);
            isUpate=false;
        }else{
            hostApply=selectByPrimaryKey(hostApplyVo.getId());
            if(ObjectUtil.isNull(hostApply)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostApply.setUpdateTime(new Date());
            hostApply.setUpdateUser(currentUser.getId());
        }
        hostApply.setCheckStatus(hostApplyVo.getCheckStatus());
        hostApply.setCheckResult(hostApplyVo.getCheckResult());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostApply);
        }else{
            num=insert(hostApply);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }else{
            if(BaseConstant.BASE_CHECK_STATUS_YSH.equals(hostApply.getCheckStatus())){
                WebMember webMember=webMemberService.selectByPrimaryKey(hostApply.getMemberId());
                webMember.setGrade("2");
                webMember.setUpdateTime(new Date());
                webMember.setUpdateUser(currentUser.getId());
                webMemberService.updateByPrimaryKey(webMember);
            }
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-5 22:09:46
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostApply hostApply=selectByPrimaryKey(id);
        if(hostApply!=null){
            hostApply.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostApply.setUpdateTime(new Date());
            hostApply.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostApply);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostApplyVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-5 22:09:46
     */
    public List<HostApply> acceptList(HostApplyVo hostApplyVo){
        Example example = new Example(HostApply.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostApplyVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostApplyVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(hostApplyVo.getMemberId())){
            criteria.andEqualTo("memberId",hostApplyVo.getMemberId());
        }
        example.setOrderByClause(" update_time desc");
        return hostApplyDao.selectByExample(example);
    }
}
